package com.swxc.ssm.controller;

import com.swxc.ssm.po.Category;
import com.swxc.ssm.po.Course;
import com.swxc.ssm.po.User;
import com.swxc.ssm.po.Usertocou;
import com.swxc.ssm.service.CategoryService;
import com.swxc.ssm.service.CourseService;
import com.swxc.ssm.service.UserService;
import com.swxc.ssm.service.UsertocouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/28.
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private UsertocouService usertocouService;
    @Autowired
    private UserService userService;

    @RequestMapping("/deleteByPrimaryKey")
    public ModelAndView deleteByPrimaryKey(Integer ID){
        courseService.deleteByPrimaryKey(ID);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/insert")
    public ModelAndView insert(Course record){
        courseService.insert(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/insertSelective")
    public ModelAndView insertSelective(Course record){
        courseService.insertSelective(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }
    @RequestMapping("/selectByStatus")
    public ModelAndView selectByStatus(@RequestParam(value="id", required = false) String id){

        List<Course> courses = courseService.selectByStatus(Integer.parseInt(id));
        List<Category> categories = categoryService.selectAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("courses",courses);
        modelAndView.addObject("categorys",categories);

        List<String> list_class = new ArrayList<String>();
        list_class.add("iconfont mooc-quanbu");list_class.add("iconfont mooc-jisuanji");list_class.add("iconfont mooc-icon");list_class.add("iconfont mooc-xinli");list_class.add("iconfont mooc-waiyu");
        list_class.add("iconfont mooc-wenxuezhexuelishi");list_class.add("iconfont mooc-yishu");list_class.add("iconfont mooc-xuegongduiwuguanli");list_class.add("iconfont mooc-lilun");list_class.add("iconfont mooc-shengmingkexue");list_class.add("iconfont mooc-shu");
        list_class.add("iconfont mooc-falv");list_class.add("iconfont mooc-jiaoyu");list_class.add("iconfont mooc-qita");
        modelAndView.addObject("list_class",list_class);

        modelAndView.setViewName("course-index");
        return modelAndView;

    }

    @RequestMapping("/selectByPrimaryKey")
    public ModelAndView selectByPrimaryKey(@RequestParam(value="id", required = false) String id){

        Course course = courseService.selectByPrimaryKey(Integer.parseInt(id));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("course",course);
        modelAndView.setViewName("course-info");
        return modelAndView;
    }

    @RequestMapping("/selectAll")
    public ModelAndView selectAll(HttpSession session){
        String before_page = (String) session.getAttribute("before_page");
        ModelAndView modelAndView = new ModelAndView();

        if(before_page==null){
            List<Course> courses = courseService.selectAll();
            List<Category> categories = categoryService.selectAll();

            modelAndView.addObject("courses",courses);
            modelAndView.addObject("categorys",categories);

            List<String> list_class = new ArrayList<String>();
            list_class.add("iconfont mooc-quanbu");list_class.add("iconfont mooc-jisuanji");list_class.add("iconfont mooc-icon");list_class.add("iconfont mooc-xinli");list_class.add("iconfont mooc-waiyu");
            list_class.add("iconfont mooc-wenxuezhexuelishi");list_class.add("iconfont mooc-yishu");list_class.add("iconfont mooc-xuegongduiwuguanli");list_class.add("iconfont mooc-lilun");list_class.add("iconfont mooc-shengmingkexue");list_class.add("iconfont mooc-shu");
            list_class.add("iconfont mooc-falv");list_class.add("iconfont mooc-jiaoyu");list_class.add("iconfont mooc-qita");
            modelAndView.addObject("list_class",list_class);

            modelAndView.setViewName("course-index");
        }else{

            String courseid = (String) session.getAttribute("courseid");
            Course course= courseService.selectByPrimaryKey(Integer.parseInt(courseid));
            modelAndView.addObject("course",course);
            modelAndView.setViewName(before_page);
            session.removeAttribute("before_page");
            session.removeAttribute("courseid");
        }
        return modelAndView;
    }

   @RequestMapping("/toLearn")
   public ModelAndView toLearn(@RequestParam(value="learnid", required = false) String learnid,@RequestParam(value="userid", required = false) String userid,@RequestParam(value="joinid", required = false) String joinid){
       ModelAndView modelAndView = new ModelAndView();

      if (learnid != null){
        Course course = courseService.selectByPrimaryKey(Integer.parseInt(learnid));
        modelAndView.addObject("course",course);
        modelAndView.setViewName("course-list");
      }else{
          usertocouService.insert(Integer.parseInt(userid),Integer.parseInt(joinid));
          Course course = courseService.selectByPrimaryKey(Integer.parseInt(joinid));
          modelAndView.addObject("course",course);
          modelAndView.setViewName("course-list");
      }
       return modelAndView;
   }

    @RequestMapping("/toLearnname")
    public ModelAndView toLearn_name(@RequestParam(value="learnname", required = false) String learnname){
        ModelAndView modelAndView = new ModelAndView();
        //System.out.print("------------"+learnname+"--------------");
        Course course = courseService.selectByName(learnname);
       // System.out.print("------------"+course+"--------------");
        modelAndView.addObject("course",course);
        modelAndView.setViewName("course-list");
        return modelAndView;
    }

    @RequestMapping("/abandon_course")
    public ModelAndView abandon_course(@RequestParam(value="abandon", required = false) Integer abandon,HttpSession session){

        usertocouService.deleteByPrimaryKey(abandon);
        ModelAndView modelAndView = new ModelAndView();

        return selectAll(session);

    }

    @RequestMapping("/search")
    public ModelAndView search(@RequestParam(value = "search_input",required = false) String search_input){
        ModelAndView modelAndView = new ModelAndView();
        List<Course> search_list = courseService.search(search_input);
        List<Category> categories = categoryService.selectAll();
        modelAndView.addObject("courses",search_list);
        modelAndView.addObject("categorys",categories);

        List<String> list_class = new ArrayList<String>();
        list_class.add("iconfont mooc-quanbu");list_class.add("iconfont mooc-jisuanji");list_class.add("iconfont mooc-icon");list_class.add("iconfont mooc-xinli");list_class.add("iconfont mooc-waiyu");
        list_class.add("iconfont mooc-wenxuezhexuelishi");list_class.add("iconfont mooc-yishu");list_class.add("iconfont mooc-xuegongduiwuguanli");list_class.add("iconfont mooc-lilun");list_class.add("iconfont mooc-shengmingkexue");list_class.add("iconfont mooc-shu");
        list_class.add("iconfont mooc-falv");list_class.add("iconfont mooc-jiaoyu");list_class.add("iconfont mooc-qita");
        modelAndView.addObject("list_class",list_class);
        modelAndView.setViewName("course-index");
        return modelAndView;
    }


    @RequestMapping("/updateByPrimaryKeySelective")
    public ModelAndView updateByPrimaryKeySelective(Course record){
        courseService.updateByPrimaryKeySelective(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @RequestMapping("/updateByPrimaryKey")
    public ModelAndView updateByPrimaryKey(Course record){
        courseService.updateByPrimaryKey(record);
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

}
